package com.soushangxing.comfyuibackendv2.controller

import com.soushangxing.comfyuibackendv2.bean.GlobalQueue
import com.soushangxing.comfyuibackendv2.bean.ResponseType
import com.soushangxing.comfyuibackendv2.entity.FileEntity
import com.soushangxing.comfyuibackendv2.entity.OrderEntity
import com.soushangxing.comfyuibackendv2.service.*
import jakarta.servlet.http.HttpServletRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile


@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("test")
class TestController {

    @Autowired
    lateinit var userService: UserService
    @Autowired
    lateinit var comfyService: ComfyUIService
    @Autowired
    lateinit var queueService: GlobalQueueService

    @Autowired
    lateinit var globalQueue: GlobalQueue


    /**
     * 测试链接
     */
    @GetMapping("test")
    fun test(): ResponseType {
        return ResponseType(null,0,"链接成功。")
    }

    /**
     * 上传图片
     */
    @PostMapping("upload")
    fun uploadImage(file: MultipartFile, request: HttpServletRequest): ResponseType {
        val userId = userService.getIdByRequest(request)
        if (userId.isNullOrEmpty()){
            return ResponseType(null,2,"登录已过期")
        }
        return comfyService.uploadFileAliyun(file)
    }

    /**
     * 测试队列功能-添加任务
     */
    @GetMapping("queue-add")
    fun queueAdd(taskId:String, request: HttpServletRequest): ResponseType {
        queueService.backendQueue(taskId,"TESTUSER","TESTPROMPT")
        return ResponseType(globalQueue.globalBackendQueue)
    }

    /**
     * 测试队列功能-执行任务
     */
    @GetMapping("queue-start")
    fun queueTest(taskId:String, serverUrl:String, request: HttpServletRequest): ResponseType {
        queueService.queuePromptStart(taskId, serverUrl)
        return ResponseType(globalQueue.globalBackendQueue)
    }


    /**
     * 测试队列功能-查看所有队列
     */
    @GetMapping("queue-list")
    fun queueList(request: HttpServletRequest): ResponseType {
        return ResponseType(globalQueue.globalBackendQueue)
    }



    /**
     * 返回一个结果，用于前端页面测试
     */
    @GetMapping("result-test")
    fun resultTest(request: HttpServletRequest): ResponseType {
        val testResult = OrderEntity()
        testResult.id = "test"
        testResult.outputPath = "https://forest-cloud.awsl.info/f/yyvsD/kazakov.png"
        return ResponseType(testResult,0,"测试请求成功")
    }
}
